package com.lottery.im.module.chat.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lottery.im.R;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.tim.model.GroupInfo;
import com.lottery.im.module.tim.model.GroupProfile;
import com.lottery.im.util.transformations.CropCircleTransformation;
import com.tencent.imsdk.TIMCallBack;
import com.tencent.imsdk.TIMGroupManager;
import java.util.ArrayList;
import java.util.List;

/**
 * 显示搜索出来的群信息
 */
public class SearchGroupProfileFragment extends BaseSwipeFragment {

  private final static String PROFILE_KEY = "PROFILE_KEY";

  public static SearchGroupProfileFragment create(GroupProfile profile) {
    SearchGroupProfileFragment fragment = new SearchGroupProfileFragment();
    Bundle b = new Bundle();
    b.putSerializable(PROFILE_KEY, profile);
    fragment.setArguments(b);
    return fragment;
  }

  @BindView(R.id.tv_title) TextView mTvTitle;
  @BindView(R.id.iv_portrait) ImageView mIvPortrait;
  @BindView(R.id.tv_name) TextView mTvName;
  @BindView(R.id.tv_num) TextView mTvNum;
  @BindView(R.id.grid) RecyclerView mGrid;
  @BindView(R.id.btn_apply) Button mBtnApply;

  private GroupProfile mProfileSummary;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_search_group_profile;
  }

  @Override protected void initPresenter() {
    mTvTitle.setText("群聊资料");
    mProfileSummary = (GroupProfile) getArguments().getSerializable(PROFILE_KEY);
    mTvName.setText(mProfileSummary.getName());
    Glide.with(mContext)
        .load(mProfileSummary.getAvatarUrl())
        .apply(new RequestOptions()
            .bitmapTransform(new CropCircleTransformation())
            .placeholder(R.drawable.icon_conversation_de)
            .error(R.drawable.icon_conversation_de))
        .into(mIvPortrait);

    mTvNum.setText("共" + mProfileSummary.getMemberNum() + "人");

    if (GroupInfo.getInstance().isInGroup(mProfileSummary.getIdentify())) {
      mBtnApply.setVisibility(View.GONE);
    } else {
      mBtnApply.setVisibility(View.VISIBLE);
    }

    List<String> items = new ArrayList<>();
    for (int i = 0; i < (mProfileSummary.getMemberNum() > 5 ? 5 : mProfileSummary.getMemberNum());
        i++) {
      items.add("");
    }

    mGrid.setLayoutManager(new GridLayoutManager(getActivity(), 5));
    mGrid.setAdapter(new SecretMemberAdapter(items));
  }

  @OnClick({ R.id.iv_back, R.id.btn_apply }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.btn_apply:
        showLoadingDialog();
        TIMGroupManager.getInstance().applyJoinGroup(mProfileSummary.getIdentify(), "",
            new TIMCallBack() {
              @Override public void onError(int i, String s) {
                 cancelLoadingDialog();
                 showToast("申请失败，请稍后再试～");
              }

              @Override public void onSuccess() {
                cancelLoadingDialog();
                showToast("申请成功");
              }
            });
        break;
    }
  }

  class SecretMemberAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

    public SecretMemberAdapter(@Nullable List<String> data) {
      super(R.layout.item_secret_member, data);
    }

    @Override protected void convert(BaseViewHolder helper, String item) {

    }
  }
}
